﻿using System.Diagnostics;
using System.Windows.Controls;

namespace EnvControlLib
{
    /// <summary>
    /// EnvVideoPanel.xaml 的交互逻辑
    /// </summary>
    public partial class EnvVideoPanel : UserControl
    {
        #region 构造函数

        public EnvVideoPanel()
        {
            InitializeComponent();

            this.DataContext = new ViewModel_EnvVideoPanel(plVideoScreen); // 注：VM 里要 Load 后才能得到 Panel，所以先设此值
        }

        #endregion

        #region 事件【注：WinFormControls 无法使用 i:Interaction.Triggers】

        /// <summary>
        /// 双击视频窗口事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void plVideoScreen_MouseDoubleClick(object sender,
            System.Windows.Forms.MouseEventArgs e)
        {
            if (this.DataContext != null &&
                this.DataContext is ViewModel_EnvVideoPanel)
            {
                (this.DataContext as ViewModel_EnvVideoPanel).videoPanelControl_MouseDoubleClick(videoPanelControl,
                    null); // 可最大化也可最小化视频窗口
            }
        }

        /// <summary>
        /// 右键菜单功能项【Panel 右键菜单】
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuItem_Click(object sender, System.EventArgs e)
        {
            if (this.DataContext != null &&
                this.DataContext is ViewModel_EnvVideoPanel)
            {
                (this.DataContext as ViewModel_EnvVideoPanel).CallMenuItem(
                    (sender as System.Windows.Forms.MenuItem).Name);
            }
        }

        /// <summary>
        /// 右键菜单的显示【Panel 右键菜单】
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelContextMenu_Popup(object sender, System.EventArgs e)
        {
            try
            {
                System.Windows.Forms.ContextMenu panelMenu = sender as System.Windows.Forms.ContextMenu;
                foreach (System.Windows.Forms.MenuItem item in panelMenu.MenuItems)
                {
                    item.Enabled = true; // 菜单全部可用
                }

                if (this.DataContext != null &&
                    this.DataContext is ViewModel_EnvVideoPanel)
                {
                    ViewModel_EnvVideoPanel vm_EnvVideoPanel = this.DataContext as ViewModel_EnvVideoPanel;

                    //////////////////////////////////////////////////////////////////////////
                    // 注：若要进行权限控制，可在 ViewModel_EnvVideoPanel 里加个公开的属性判断，如：IsRealPlayPanel
                    //////////////////////////////////////////////////////////////////////////
                    if (string.IsNullOrWhiteSpace(vm_EnvVideoPanel.VideoInformation)) // 无标题数据时
                    {
                        foreach (System.Windows.Forms.MenuItem item in panelMenu.MenuItems)
                        {
                            item.Enabled = false; // 菜单全部不可用
                        }
                    }
                    else if (vm_EnvVideoPanel.IsMaxPanel) // 最大化视频窗口时
                    {
                        panelMenu.MenuItems["closeAllPanel"].Enabled = false; // “关闭所有视频”菜单不可用
                    }

                    if (vm_EnvVideoPanel.IsRealPlayPanel != System.Windows.Visibility.Visible)
                    {
                        panelMenu.MenuItems["recordVideo"].Visible = false; // “视频流录像”菜单不可见
                        panelMenu.MenuItems["screenShot"].Visible = false; // “视频窗口截图”菜单不可见
                        panelMenu.MenuItems["playBack"].Visible = false; // “远程录像回放”菜单不可见
                    }
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}
